package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.common.R;
import com.peng.dto.DishDto;
import com.peng.dto.SetmealDto;
import com.peng.entity.Dish;
import com.peng.entity.Setmeal;
import com.peng.entity.SetmealDish;
import com.peng.handler.CustomException;
import com.peng.service.SetmealDishService;
import com.peng.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("/page")
    public R<Page<SetmealDto>> pageR(Integer page, Integer pageSize, String name){
        Page<SetmealDto> page1=setmealService.pageR(page,pageSize,name);
        return R.success(page1);
    }

    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> update(@PathVariable("id") Long  id){
        Setmeal byId = setmealService.getById(id);
        List<SetmealDish> one = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);
        setmealDto.setSetmealDishes(one);
        return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> saveAndUpdate(@RequestBody SetmealDto dto){
        setmealService.updateById(dto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,dto.getId());
        setmealDishService.remove(queryWrapper);
        for (SetmealDish setmealDish : dto.getSetmealDishes()) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(dto.getSetmealDishes());
        return R.success("修改成功");

    }


    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam("ids")List<Long> ids){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        if(setmealService.count(setmealLambdaQueryWrapper)>0)
            throw new CustomException("存在套餐正在售卖中,不能删除");
        setmealService.removeByIds(ids);
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(queryWrapper);
        }
        return R.success("删除成功");
    }

    @PostMapping("/status/{sta}")
    public R<String> statusIds(@PathVariable("sta") Integer sta ,@RequestParam("ids") List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(sta);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }


    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }

}
